;
(function (window, angular) {
  'use strict';
  angular.module("Controller.Enterprise.Technician", [])
    .controller("TechnicianCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService) {
      $scope.activeFlag = [true, false, false];
      $scope.markInfo = {
        allChecked: false
      };
      $scope.pageInfo = {
        totalPages: 1,
        totalRows: 0,
        pageSize: 10,
        curPage: 1
      };
      $scope.submitted = false;
      $scope.toolbarConfig = {
        showAdvanceSearch: false,
        showColConfig: false
      };
      $scope.condition = {};
      $scope.dataList = [];
      $scope.detailInfo = { status: 1 };
      $scope.tabFlag = {};
      var tabFlag = {
        0: "新增",
        1: "编辑"
      };

      $scope.tableConfig = {
        scrollWidth: 1000,
        fixedWidth: 260,
        tableCol: [
          {
            name: "手机号",
            dataField: "mobilePhone",
            width: 100,
            isShow: true,
            isFixed: false,
          },
          {
            name: "姓名",
            dataField: "cname",
            width: 100,
            isShow: true,
            isFixed: false,
          },
          {
            name: "服务站",
            dataField: "orgName",
            width: 200,
            isShow: true,
            isFixed: false,
          },
          {
            name: "状态",
            dataField: "statusShow",
            width: 80,
            isShow: true,
            isFixed: false,
          },
          {
            name: "最后登录时间",
            dataField: "lastLoginTimeShow",
            width: 140,
            isShow: true,
            isFixed: false,
          },
          {
            type: "operate",
            name: "操作",
            isShow: true,
            isFixed: true,
          }
        ]
      };

      var btnPermissionCode = {
        technicianAdd: window.ActionCode.technicianAdd,
        technicianEdit: window.ActionCode.technicianEdit,
        technicianEnable: window.ActionCode.technicianEnable,
        technicianDisable: window.ActionCode.technicianDisable,
        technicianResetPwd: window.ActionCode.technicianResetPwd,
        technicianRestWxBind: window.ActionCode.technicianRestWxBind,
      };

      $scope.btnPermission = {
        technicianAdd: true,
        technicianEdit: true,
        technicianEnable: true,
        technicianDisable: true,
        technicianResetPwd: true,
        technicianRestWxBind: true,
      };

      function setBtnPermission() {
        for (var key in btnPermissionCode) {
          $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
        }
      }

      $scope.setFixedWidth = function () {
        $scope.tbStyle = { scrollLeft: { left: $scope.tableConfig.fixedWidth + "px" } };
      };

      $scope.changeTab = function (index) {
        $scope.activeFlag = [false, false, false];
        $scope.activeFlag[index] = true;
        $scope.submitted = false;
        switch (index) {
          case 0:
            initData();
            break;
          case 1:
            break;
          case 2:
            break;
          default:
            break;
        }
      };

      function initData() {
        $scope.tabFlag = {};
        $scope.detailInfo = { status: 1 };
      }

      function getDataList(curPage) {
        var service = "mt";
        var reqUrl = "/technician/list";
        var param = {};
        if (!!curPage) {
          $scope.pageInfo.curPage = curPage;
        }
        param.pageSize = $scope.pageInfo.pageSize;
        param.pageNum = $scope.pageInfo.curPage;
        param.filters = angular.copy($scope.condition);
        ybHttp.Post(service, reqUrl, param).then(function (resp) {
          if (resp.code !== 0) {
            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
            return;
          }
          $scope.markInfo.allChecked = false;
          angular.forEach(resp.value.list, function (item) {
            item.isChecked = false;
            item.statusShow = $filter("TrueOrFalse")(item.status, "status");
            item.lastLoginTimeShow = $filter("YbDate")(item.lastLoginTime, "yyyy-MM-dd HH:mm:ss");
          });
          $scope.pageInfo.totalRows = resp.value.total;
          $scope.pageInfo.totalPages = resp.value.pages;
          $scope.dataList = resp.value.list;
        });
      }

      $scope.search = function (curpage) {
        getDataList(curpage);
      };

      $scope.switchToolbar = function (toolbar) {
        if (!!$scope.toolbarConfig["show" + toolbar]) {
          $scope.toolbarConfig["show" + toolbar] = false;
        } else {
          $timeout(function () {
            $scope.toolbarConfig["show" + toolbar] = true;
          });
        }
      };

      $scope.selectRow = function (index) {
        $scope.markInfo.curRow = index
      };

      $scope.reset = function () {
        $scope.condition = {};
      };

      $scope.selectAll = function () {
        for (var i = 0, len = $scope.dataList.length; i < len; i++) {
          $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
        }
        getUserIds();
      };

      function checkAll() {
        var selectAll = false;
        for (var i = 0, len = $scope.dataList.length; i < len; i++) {
          if (!$scope.dataList[i].isChecked) {
            selectAll = false;
            break;
          }
          selectAll = true;
        }
        return selectAll;
      }

      $scope.selectSingle = function (item) {
        if (!item.isChecked) {
          $scope.markInfo.allChecked = false;
        } else {
          if (!checkAll()) {
            $scope.markInfo.allChecked = false;
          } else {
            $scope.markInfo.allChecked = true;
          }
        }
        getUserIds();
      };

      $scope.addData = function () {
        $scope.tabFlag.type = 0;
        $scope.tabFlag.name = tabFlag[0];
        $scope.changeTab(1);
      };

      $scope.editData = function (user) {
        $scope.detailInfo = angular.copy(user);
        $scope.tabFlag.type = 1;
        $scope.tabFlag.name = tabFlag[1];
        $scope.changeTab(1);
      };

      $scope.restPwd = function (val) {
        var service = "mt";
        var reqUrl = "/technician/resetPassword";
        var param = {
          id: val
        };
        ybWidget.ShowMessage("确定将密码重置为xcmg123.吗？", MESG_TYPE.Ask).then(function () {
          ybHttp.Get(service, reqUrl, param).then(function (resp) {
            if (resp.code !== 0) {
              ybWidget.ShowMessage(resp, MESG_TYPE.Error);
              return;
            }
            ybWidget.ShowToaster('success', '操作成功');
          });
        }, function () {

        });
      };

      $scope.switchStatus = function (val, statusType) {
        var service = "mt";
        var reqUrl = "";
        var param = {
          id: val
        };
        var tipMsg = "";

        if (statusType == "0") {
          reqUrl = '/technician/updateDisable'
          tipMsg = "停用";
        } else if (statusType == "1") {
          reqUrl = '/technician/updateEnable'
          tipMsg = "启用";
        }

        ybWidget.ShowMessage("确定" + tipMsg + "该技师吗？", MESG_TYPE.Ask).then(function () {
          ybHttp.Post(service, reqUrl, param).then(function (resp) {
            if (resp.code !== 0) {
              ybWidget.ShowMessage(resp, MESG_TYPE.Error);
              return;
            }
            ybWidget.ShowToaster('success', '操作成功');
            getDataList();
          });
        }, function () { });
      };

      $scope.saveData = function (isValid) {
        if (!isValid) {
          $scope.submitted = true;
          return;
        }
        var service = "mt";
        var reqUrl = "";
        var param = angular.copy($scope.detailInfo);
        if ($scope.tabFlag.type == 0) {
          reqUrl = "/technician/insert";
        } else if ($scope.tabFlag.type == 1) {
          reqUrl = "/technician/update";
        }
        ybHttp.Post(service, reqUrl, param).then(function (resp) {
          if (resp.code !== 0) {
            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
            return;
          }
          ybWidget.ShowToaster('success', '操作成功');
          $scope.changeTab(0);
          getDataList();
        });
      };

      $scope.exportParam = {
        sectionUrl: ybHttp.RequestUrl("device", "/user/export"),
        sectionParam: {
          ids: []
        },
        allUrl: ybHttp.RequestUrl("device", "/user/exportAll")
      };

      function getUserIds() {
        $scope.exportParam.sectionParam.ids = [];
        angular.forEach($scope.dataList, function (item) {
          if (!!item.isChecked) {
            $scope.exportParam.sectionParam.ids.push(item.id);
          }
        });
      }

      $scope.restWxBindWithPhone = function(id) {
        ybWidget.ShowMessage("确定重置该技师手机号和已绑定的微信号之间的关系吗？", MESG_TYPE.Ask).then(function () {
          var service = "mt";
          var reqUrl = "/technician/resetWxBind";
          ybHttp.Post(service, reqUrl, [id]).then(function (resp) {
            if (resp.code !== 0) {
              ybWidget.ShowMessage(resp, MESG_TYPE.Error);
              return;
            }
            ybWidget.ShowToaster('success', '操作成功');
          });
        }, function () { });
      };

      (function () {
        getDataList();
        $scope.setFixedWidth();
        UserContextService.SetBtnPermission(setBtnPermission);
        if (!$scope.isYingXiao) {
          $scope.tableConfig.tableCol = ybWidget.HideTableCols($scope.tableConfig.tableCol, ['orgName']);
        }
      })();
    }])
    ;
})(window, window.angular);